import logging
from functools import partial

from PyQt5.QtWidgets import QTableWidgetItem, QHeaderView

from .pagevac_base import SubPageWithTableVac
from ioslave.slave import get_pin_value, get_pin_forced_value
from .pagevac_udf import LIST_IO_UDF_MARK

class PLC_PageVac(SubPageWithTableVac):
    def __init__(self, parent=None):
        self.table_group_num = 2  # 定义表格每行并行显示几组数据
        self.table_group_item_num = 0  # 根据定义的数据，表示每组数据的数据项数目

        def table_update():
            curr_stack_index = self.stack_wx.currentIndex()
            if curr_stack_index == 0:  return
            elif curr_stack_index == 1:
                self.table_data_update(self.curr_table_wx(), True)
            elif curr_stack_index == 2:
                self.table_data_update(self.curr_table_wx(), False)
            else:
                raise Exception()

        list_tab_names = ["PLC全览", "输入地址", "输出地址"]
        super().__init__(parent, list_tab_names)
        self.tab_bar.on_tab_btn_clicked_extend(table_update)
        self._active()

    def _active(self):
        self.udf_btn.clicked.connect(self.on_udf_btn_clicked)

    def table_data_update(self, table_wx, isInput):
        self.table_data_clear(table_wx)  # 清空数据

        io_addr_start = 0
        io_addr_end = 8

        row_index = -1
        for addr_byte in range(io_addr_start, io_addr_end, 1):
            addr_bit = -1
            # for addr_bit in range(0, IO_BIT_LENGTH, self.table_group_num):
            while(addr_bit < 7):  # IO_BIT_LENGTH-1 ??
                row_index += 1
                list_column_strings = []
                for _ in range(self.table_group_num):
                    addr_bit = addr_bit + 1  # if addr_bit < 7 else 0
                    io_address = "{}.{}".format(addr_byte, addr_bit)
                    panel_prefix = "IX" if isInput else "QX"
                    panel_io_name = "PLC." + panel_prefix + io_address

                    list_column_string = [
                        panel_io_name,
                        io_address,
                        get_pin_value(panel_io_name)
                    ]
                    if not self.table_group_item_num:
                        self.table_group_item_num = len(list_column_string)
                    list_column_strings.extend(list_column_string)

                table_wx.insertRow(row_index)  # 动态添加行
                for column_index in range(len(list_column_strings)):
                    # 动态写入单元格数据
                    table_wx.setItem(row_index, column_index, 
                            QTableWidgetItem(list_column_strings[column_index]))

    def on_udf_btn_clicked(self):
        stack_page_index = self.show_stack_wx.currentIndex()
        table_wx = self.list_subpage_table[stack_page_index][0]
        current_row = table_wx.currentRow()
        current_column = table_wx.currentColumn()

        group_index = int(current_column / self.table_group_item_num)
        current_ioaddress_column = group_index * self.table_group_item_num
        table_item_text = table_wx.item(current_row, current_ioaddress_column).text()

        if table_item_text not in LIST_IO_UDF_MARK:
            LIST_IO_UDF_MARK.append(table_item_text)
